package com.fqgj.sentry.statistics.domain;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.sentry.policy.dao.PcExecutorDao;
import com.fqgj.sentry.policy.dao.PcPolicySetsDao;
import com.fqgj.sentry.policy.entity.PcExecutorEntity;
import com.fqgj.sentry.policy.entity.PcPolicySetsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-08-18 下午3:28
 */

@Component
public class CommonsService {

    @Autowired
    private PcPolicySetsDao pcPolicySetsDao;
    @Autowired
    private PcExecutorDao pcExecutorDao;

    //获取线上策略集版本的执行者id(统计只包含挑战者和冠军者)
    public String getCurrentExecutorIds(Long appId) {
        List<PcPolicySetsEntity> pcPolicySetsEntities = pcPolicySetsDao.selectByAppIdsAndPage(appId);
        if (CollectionUtils.isEmpty(pcPolicySetsEntities)) {
            return null;
        }
        List<PcExecutorEntity> list = new ArrayList<>();
        for (PcPolicySetsEntity policySetsEntity : pcPolicySetsEntities) {
            PcExecutorEntity pcExecutorEntity = pcExecutorDao.selectPcExecutorCurrent(policySetsEntity.getId(), 2, 2);
            if (pcExecutorEntity != null) {
                list.add(pcExecutorEntity);
            }
            PcExecutorEntity pcExecutorEntity2 = pcExecutorDao.selectPcExecutorCurrent(policySetsEntity.getId(), 3, 2);
            if (pcExecutorEntity2 != null) {
                list.add(pcExecutorEntity2);
            }
        }
        StringBuffer stringBuffer = new StringBuffer("");
        for (int i = 0, size = list.size(); i < size; i++) {
            if (i == (size - 1)) {
                stringBuffer.append(list.get(i).getId());
            } else {
                stringBuffer.append(list.get(i).getId()).append(",");
            }
        }
        return stringBuffer.toString();
    }

}
